<template>
  <div class="carousel-container">
    <el-carousel :interval="3000" arrow-position="none" style="height: 600px">
      <el-carousel-item >
        <div class="banner-image"
         :style="{ backgroundImage: 'url(https://www.helloimg.com/images/2023/01/01/oCOTeQ.png)' }" 
         @load="handleImageLoad"></div>
        <div class="banner-title">你好啊</div>
      </el-carousel-item>
      <el-carousel-item >
        <div class="banner-image"
         :style="{ backgroundImage: 'url(https://www.helloimg.com/images/2022/10/12/ZgvkmY.png)' }" 
         @load="handleImageLoad"></div>
        <div class="banner-title">你好啊</div>
      </el-carousel-item>
      <el-carousel-item >
        <div class="banner-image"
         :style="{ backgroundImage: 'url(https://www.helloimg.com/images/2022/12/29/oCtxm5.png )' }" 
         @load="handleImageLoad"></div>
        <div class="banner-title">你好啊</div>
      </el-carousel-item>
      <!-- <el-carousel-item v-for="(item, index) in banner" :key="index">
        <div class="banner-image" :style="{ backgroundImage: 'url(' + item.image + ')' }" @load="handleImageLoad"></div>
        <div class="banner-title">{{ item.name }}</div>
      </el-carousel-item> -->
    </el-carousel>
  </div>
</template>

<script>
import {getBannerList} from "@/api/api.js";

export default {
  data() {
    return {
      banner: [], //杞拷锟藉浘
    };
  },
  methods: {
    fetchBannerData() {
      getBannerList()
          .then((res) => {
            this.banner = res.results;
            this.banner.forEach((item) => {
              item.image = item.image.replace(/鍘熷瓧绗︿覆/g, "鏂板瓧绗︿覆");
            });
          })
          .catch((error) => {
            console.log(error);
          });
    },
    handleImageLoad(event) {
      const img = event.target;
      img.parentElement.style.height = `${window.innerHeight}px`;

      // 寮哄埗閲嶇粯鏍峰紡
      setTimeout(() => {
        img.style.display = "none";
        img.offsetHeight;
        img.style.display = "";
      }, 0);
    },
  },
  mounted() {
    this.fetchBannerData();
  },
};
</script>

<style lang="less" scoped>
.carousel-container {
  .el-carousel__item:nth-child(odd) {
    background-color: white;
  }

  .el-carousel__item:nth-child(even) {
    background-color: #f0f0f0;
  }

  .el-carousel__item {
    transition: transform 0.5s ease-in-out;
    cursor: pointer;

    &:hover {
      transform: scale(1.05) rotateY(180deg);
    }
  }

.banner-image {
  width: 100%;
  height: 100%;
  flex-shrink: 0;
  background-size: cover !important;
  background-repeat: no-repeat;
  background-position: center center;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
}

  .banner-title {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 10px;
    text-align: center;
    font-weight: bold;
    font-size: 24px;
    color: white;
    background-color: rgba(0, 0, 0, 0.5);
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);
    transform-origin: center center;
    transform: translateX(-100%);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: transform 0.5s ease-in-out;
  }

  .el-carousel__item:hover .banner-title {
    transform: translateX(0%);
  }

  /* 杞挱椤硅繃娓″姩鐢� */

  .el-carousel__item-enter-active,
  .el-carousel__item-leave-active {
    transition: transform 0.5s ease-in-out, opacity 0.5s ease-in-out;
  }

  .el-carousel__item-enter,
  .el-carousel__item-leave-to {
    transform: translateX(100%) rotateY(90deg);
    opacity: 0;
  }
}
</style>